package base.game;

import game.base.SpriteAnimated;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import base.src.Images;

public class Map {
	private static final int rowsColumn = 10, rowsLine = 10;
	private int grid[][] = new int[rowsColumn][rowsLine];
	private ArrayList<BaseShip> ships = new ArrayList<BaseShip>();
	private Player owner;
	public static int rowWidth, rowHeight;
	public static int mapStartX = 0, mapStartY = 0;
	private Paint gridPaint;
	private SpriteAnimated explosion;
	private int timeExplod = 0;
	public Map(Player _owner) {
		owner = _owner;
		for (int i = 0; i < grid.length; i++) {
			for (int j = 0; j < grid.length; j++) {
				grid[i][j] = 0;
			}
		}
		gridPaint = new Paint();
		gridPaint.setStyle(Style.STROKE);
		gridPaint.setColor(Color.WHITE);
		gridPaint.setAlpha(150);
	}
	
	public static void calculateRowSize(float width, float height) {
		rowWidth = (int) (width / rowsColumn);
		rowHeight = (int) (height / rowsLine);
	}

	public void onDraw(Canvas canvas, Player requestDraw) {
		for (int y = 0; y < rowsLine; y++) {
			for (int x = 0; x < rowsColumn; x++) {
				float left = mapStartX + x * rowWidth;
				float top = mapStartY + y * rowHeight;
				canvas.drawRect(left, top, left + rowWidth, top + rowHeight, gridPaint);
			}
		}

		if (requestDraw != owner) {
			for (int i = 0; i < ships.size(); i++) {
				BaseShip ship = ships.get(i);
				ship.onDrawToEnemy(canvas);
			}
		} else {
			Paint p = new Paint();
			for (int i = 0; i < ships.size(); i++) {
				BaseShip ship = ships.get(i);
				ship.onDraw(canvas, p);
			}
		}
		if (explosion != null) {
			if(timeExplod<=100){
				explosion.draw(canvas);
			}
		}
	}
	
	public void onUpdate(long difftime) {
		if (explosion != null) {
			
			timeExplod+=difftime;
			if(timeExplod<=100){
				explosion.update(difftime);
			}
		}
	}
	
	public int[][] getGrid() {
		return grid;
	}
	
	public void addShip(BaseShip ship) {
		ships.add(ship);
	}
	
	public boolean isValidAndFreePosition(int mouseX, int mouseY) {
		return !(mouseX < 0 || mouseX >= rowsColumn || mouseY < 0 || mouseY >= rowsLine || grid[mouseX][mouseY] == 1);
	}
	
	public static void setStartPositionY(float height) {
		mapStartY = (int) height;
	}
	
	public static void setStartPositionX(float width) {
		mapStartX = (int) width;
	}
	
	public void shot(int touchX, int touchY) {
		if (grid[touchX][touchY] == 1) {
			explosion = new SpriteAnimated(new Point(Map.mapStartX + touchX * Map.rowWidth, Map.mapStartY + touchY * Map.rowHeight),
					new Point(64, 62), Images.explosion, 30, 25);
			for (int i = 0; i < ships.size(); i++) {
				BaseShip ship = ships.get(i);
				if (ship.collided(touchX, touchY)) {
					ship.takeDamage(touchX, touchY);
				}
			}
		} else {
			explosion = new SpriteAnimated(new Point(Map.mapStartX + touchX * Map.rowWidth, Map.mapStartY + touchY * Map.rowHeight),
					new Point(64, 62), Images.explosionWater, 30, 25);
		}
	}
}
